import okx.PublicData as PublicData
import okx.MarketData as MarketData
from okutils.rate_limiter import RateLimiter

# Replace with your SOCKS proxy server details
proxy_host = '127.0.0.1'
proxy_port = 7890

# SOCKS proxy configuration
proxies = {
    'http://': f'http://{proxy_host}:{proxy_port}',  # SOCKS5 proxy for HTTP
    'https://': f'http://{proxy_host}:{proxy_port}'  # SOCKS5 proxy for HTTPS
}
# print(proxies)

# USDT-SWAP

flag = "0"  # live trading: 0, demo trading: 1

PublicDataAPI = PublicData.PublicAPI(flag=flag, proxy=proxies)


def get_all_swap():
    """
     获取所有合约种类数据
    :return:
    """
    result = PublicDataAPI.get_instruments(instType="SWAP")
    # 获取ctValCcy为USDT的instId集合
    usdt_inst_ids = [entry['instId'] for entry in result['data'] if entry['settleCcy'] == 'USDT']
    print(usdt_inst_ids)
    return usdt_inst_ids


marketDataAPI = MarketData.MarketAPI(flag=flag, debug=False, proxy=proxies)


def rate_limited(max_requests_per_interval, interval):
    rate_limiter = RateLimiter(max_requests_per_interval, interval)
    return rate_limiter.limit()


@rate_limited(max_requests_per_interval=10, interval=2)  # 直接在装饰器上指定参数
def get_mark_price_candles(inst_id, after='', before='', bar='30m', limit=100):
    # 获取标记价格K线数据
    result = marketDataAPI.get_mark_price_candlesticks(
        instId=inst_id, bar=bar, after=after, before=before, limit=limit
    )
    return result
